package com.blq.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.blq.api.domain.Bo.MaterialAfterBo;
import com.blq.api.domain.MaterialAfter;
import com.blq.api.domain.Vo.MaterialAfterVo;
import com.blq.api.mapper.MaterialAfterMapper;
import com.blq.api.service.IMaterialAfterService;
import com.blq.common.utils.StringUtils;
import com.blq.common.core.page.TableDataInfo;
import com.blq.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Blq
 * @date 2023-01-31
 */
@RequiredArgsConstructor
@Service
public class MaterialAfterServiceImpl extends ServiceImpl<MaterialAfterMapper, MaterialAfter> implements IMaterialAfterService {

    private final MaterialAfterMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public MaterialAfterVo queryById(String materialAfter){
        return baseMapper.selectVoById(materialAfter);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<MaterialAfterVo> queryPageList(MaterialAfterBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MaterialAfter> lqw = buildQueryWrapper(bo);
        Page<MaterialAfterVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<MaterialAfterVo> queryList(MaterialAfterBo bo) {
        LambdaQueryWrapper<MaterialAfter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MaterialAfter> buildQueryWrapper(MaterialAfterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MaterialAfter> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialAfter()), MaterialAfter::getMaterialAfter, bo.getMaterialAfter());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialRepari()), MaterialAfter::getMaterialRepari, bo.getMaterialRepari());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(MaterialAfterBo bo) {
        MaterialAfter add = BeanUtil.toBean(bo, MaterialAfter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMaterialAfter(add.getMaterialAfter());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(MaterialAfterBo bo) {
        MaterialAfter update = BeanUtil.toBean(bo, MaterialAfter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MaterialAfter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
